package cn.domypay.mail.bean;

import java.io.*;
import javax.mail.internet.InternetAddress;

/**
 *
 * <p>Title: </p>
 * <p>Description: Swanet common tools</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: Swanet </p>
 * @author Tim huang
 * @version 1.0
 */

public class EmailUser implements Serializable{

  /**
	 * 
	 */
	private static final long serialVersionUID = 7964526128353463248L;
	
	private String address;
	private String personal;

  public EmailUser(){
  }

  public EmailUser(String address){
      this.address = address;
  }

  public EmailUser(String address, String personal){
      this.address = address;
      this.personal = personal;
  }

  public static EmailUser copyFrom(InternetAddress in){
    if(in == null){
      return null;
    } else{
      EmailUser user = new EmailUser();
      String addr = in.getAddress().replaceAll("'", "");
      user.setAddress(addr);
      user.setPersonal(in.getPersonal());
      return user;
    }
  }
  /**
   *
   * @return InternetAddress
   */
  public InternetAddress copyTo(){
    try{
      InternetAddress ia = new InternetAddress(address, personal);
      return ia;
    }catch(UnsupportedEncodingException e){
      System.err.println("unsupported encoding when convert EmailUser to InternetAddress.");
      e.printStackTrace();
    }
    return null;
  }

  public String getAddress(){
      return address;
  }

  public String getPersonal(){
      return personal;
  }

  public void setAddress(String string){
      address = string;
  }

  public void setPersonal(String string){
      personal = string;
  }

  public String toString(){
    if(personal == null || personal.trim().length() <= 0){
      return address;
    } else{
      String s = personal + "<" + address + ">";
      return s;
    }
  }

  public boolean equals(Object obj){
    if(obj instanceof EmailUser){
      EmailUser user = (EmailUser)obj;
      return address.equalsIgnoreCase(user.getAddress());
    } else{
      return false;
    }
  }

}
